module page {

    export class AdPage extends egret.Sprite {

        private bannerImg: egret.Bitmap;

        private floatImg: egret.Bitmap;

        private floatImg2: egret.Bitmap;

        public adList = [];

        private ad_imgs = [];

        private count = 0;

        public list;

        private arrow: egret.Bitmap;

        private shp_bg: egret.Shape;

        private isLoading: boolean = false;

        public floatAd;

        private floatAdArr = [];

        public static readonly BANNER_RESIZE: string = "BannerResize";

        public constructor() {
            super();
            let adManager = manager.GameManager.getInstance().getAdManager();
            this.adList = adManager.drawer_ad || [];
        }

        public switchBanner(visible): void {
            let adManager = manager.GameManager.getInstance().getAdManager();
            if (adManager.bannerAd) {
                if (visible) {
                    adManager.bannerAd.show();
                } else {
                    adManager.bannerAd.hide();
                }
            } else {
                if (!this.bannerImg) {
                    return;
                }
                this.bannerImg.visible = visible;
            }
        }


        private setBannerAd() {
            let adManager = manager.GameManager.getInstance().getAdManager();
            this.bannerImg = new egret.Bitmap();
            this.bannerImg.touchEnabled = true;
            this.bannerImg.addEventListener(egret.TouchEvent.TOUCH_TAP, function (eve: egret.Event) {
                eve.stopImmediatePropagation();
                adManager.showAd("banner_ad");
            }, this);
        }

        private createAd(index) {
            let bgImgUrl: string = this.adList[index].img;
            let imgLoader = new egret.ImageLoader();
            imgLoader.addEventListener(egret.Event.COMPLETE, this.loadAd.bind(this, index), this);
            imgLoader.load(bgImgUrl);
        }


        private loadAd(index, event: egret.Event) {
            let imageLoader = <egret.ImageLoader>event.currentTarget;
            let texture = new egret.Texture();
            texture._setBitmapData(imageLoader.data);
            let bitmap = new egret.Bitmap();
            bitmap.$setTexture(texture);
            this.ad_imgs[index] = bitmap;
            this.count++;
            if (this.count >= this.adList.length) {
                this.count = 0;
                manager.GameManager.getInstance().statisticManager.adStats(null, "drawer");
            }
        }

        private switchAdList(eve: egret.Event): void {
            eve.stopImmediatePropagation();
            if (this.list.x < 0) {
                this.shp_bg.visible = true;
                egret.Tween.get(this.list).to({x: 0}, 300);
            } else {
                this.shp_bg.visible = false;
                egret.Tween.get(this.list).to({x: -this.list.width}, 300);
            }
        }


        public clickAd(index, eve) {
            console.log("clickAd:", index);
            if (eve) {
                eve.stopImmediatePropagation();
            }
            Main.getInstance().gameManager.adManager['drawer_ad_idx'] = index;
            manager.GameManager.getInstance().getAdManager().showAd("drawer_ad");
        }

        private loadImg(): void {
            if (this.isLoading) {
                return;
            }
            egret.log("loadImg");
            this.isLoading = true;
            let that = this;
            let adManager = manager.GameManager.getInstance().getAdManager();
            let bannerAd = adManager.banner_ad;
            if (bannerAd == null || bannerAd.length < 1) {
                egret.log("loadImg no banner img url");
                return;
            }
            let bannerImgURL = bannerAd[0].img;
            let bgImgUrl: string = bannerImgURL;
            let imgLoader = new egret.ImageLoader();
            imgLoader.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                let imageLoader = <egret.ImageLoader>event.currentTarget;
                that.updateTexture(imageLoader.data);
                that.isLoading = false;
                egret.log("广告显示banner");
            }, this);
            imgLoader.load(bgImgUrl);
            manager.GameManager.getInstance().statisticManager.adStats(null, "banner");
        }

        private updateTexture(bitmapData: egret.BitmapData): void {
            let texture = new egret.Texture();
            texture.bitmapData = bitmapData;
            this.bannerImg.texture = texture;
            this.addChild(this.bannerImg);
            let scale: number = manager.DisplayManager.STAGE_WIDTH / this.bannerImg.width * 0.7;
            this.bannerImg.scaleX = this.bannerImg.scaleY = scale;
            let realHeight: number = this.bannerImg.height * scale;
            this.bannerImg.x = this.bannerImg.width * 0.15;
            this.bannerImg.y = manager.DisplayManager.STAGE_HEIGHT - realHeight;
            this.dispatchEvent(new egret.Event(AdPage.BANNER_RESIZE, false, false, realHeight));
        }

        public floatAd2;
        private floatAdArr2 = [];

        private setFloatAd2(): void {
            console.log('setFloatAd2setFloatAd2setFloatAd2setFloatAd2setFloatAd2')
            let adManager = manager.GameManager.getInstance().getAdManager();
            let float_ad = adManager.float_ad2;
            if(!float_ad || !float_ad.length) {
                return;
            }
            let that = this;
            let len = float_ad.length;
            let index = Main.getInstance().gameManager.adManager['float_ad2_idx'] % len;
            let switch_time = Number(adManager.switch_time) || 5000;
            if (this.floatAdArr2[index]) {
                this.floatImg2.texture = this.floatAdArr2[index];
            } else {
                let img = float_ad[index]["img"] || "http://wx.11babay.cn/uploads/l/lxnkub1478500252/4/9/1/9/5b84de9eb45a7.png_p2js";
                if (img) {
                    let imgLoader = new egret.ImageLoader();
                    imgLoader.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                        let imageLoader = <egret.ImageLoader>event.currentTarget;
                        let bitmapData = imageLoader.data;
                        let texture = new egret.Texture();
                        texture.bitmapData = bitmapData;
                        this.floatAdArr2[index] = texture;
                        if (!this.floatAd2) {
                            this.floatImg2 = new egret.Bitmap();
                            this.floatImg2.width = 100;
                            this.floatImg2.height = 100;
                            this.floatImg2.texture = texture;
                            this.floatAd2 = new ui.PushButton(this.floatImg2);
                            let scale = (150 * manager.DisplayManager.STAGE_WIDTH / 750) / this.floatAd2.width;
                            this.floatAd2.playTween();
                            this.floatAd2.touchEnabled = true;
                            this.floatAd2.x = float_ad["x"] || (manager.DisplayManager.STAGE_WIDTH - this.floatAd2.width);
                            if (manager.GameManager.isProduct) {
                                this.floatAd2.visible = true;
                            }
                            this.floatAd2.y = float_ad["y"] || 180;
                            this.floatAd2.addEventListener(egret.TouchEvent.TOUCH_TAP, function (eve: egret.Event) {
                                eve.stopImmediatePropagation();
                                adManager.showAd("float_ad2");
                            }, this);
                            //this.floatAd2.scaleX = this.floatAd2.scaleY = scale;
                            this.addChild(this.floatAd2);

                            var timer:egret.Timer = new egret.Timer(switch_time, 0);
                            timer.addEventListener(egret.TimerEvent.TIMER, function () {
                                Main.getInstance().gameManager.adManager['float_ad2_idx']++;
                                this.setFloatAd2();
                            }.bind(this), this);
                            timer.start();
                        } else {
                            this.floatImg2.texture = texture;
                        }
                    }, this);
                    imgLoader.load(img);
                }
            }
        }


        private setFloatAd(): void {
            let adManager = manager.GameManager.getInstance().getAdManager();
            let float_ad = adManager.float_ad;
            if(!float_ad || !float_ad.length) {
                return;
            }
            let that = this;
            let len = float_ad.length;
            let index = Main.getInstance().gameManager.adManager['float_ad_idx'] % len;
            let switch_time = Number(adManager.switch_time) || 5000;
            if (this.floatAdArr[index]) {
                this.floatImg.texture = this.floatAdArr[index];
            } else {
                let img = float_ad[index]["img"] || "http://wx.11babay.cn/uploads/l/lxnkub1478500252/4/9/1/9/5b84de9eb45a7.png_p2js";
                if (img) {
                    let imgLoader = new egret.ImageLoader();
                    imgLoader.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                        let imageLoader = <egret.ImageLoader>event.currentTarget;
                        let bitmapData = imageLoader.data;
                        let texture = new egret.Texture();
                        texture.bitmapData = bitmapData;
                        this.floatAdArr[index] = texture;
                        if (!this.floatAd) {
                            this.floatImg = new egret.Bitmap();
                            this.floatImg.width = 100;
                            this.floatImg.height = 100;
                            this.floatImg.texture = texture;
                            this.floatAd = new ui.PushButton(this.floatImg);
                            let scale = (150 * manager.DisplayManager.STAGE_WIDTH / 750) / this.floatAd.width;
                            this.floatAd.playTween();
                            this.floatAd.touchEnabled = true;
                            this.floatAd.x = float_ad["x"] || 0;
                            if (manager.GameManager.isProduct) {
                                this.floatAd.visible = true;
                            }
                            this.floatAd.y = float_ad["y"] || 200;
                            this.floatAd.addEventListener(egret.TouchEvent.TOUCH_TAP, function (eve: egret.Event) {
                                eve.stopImmediatePropagation();
                                adManager.showAd("float_ad");
                            }, this);
                            //this.floatAd.scaleX = this.floatAd.scaleY = scale;
                            this.addChild(this.floatAd);

                            var timer:egret.Timer = new egret.Timer(switch_time, 0);
                            timer.addEventListener(egret.TimerEvent.TIMER, function () {
                                Main.getInstance().gameManager.adManager['float_ad_idx']++;
                                this.setFloatAd();
                            }.bind(this), this);
                            timer.start();
                        } else {
                            this.floatImg.texture = texture;
                        }
                    }, this);
                    imgLoader.load(img);
                }
            }
        }


        public show(need_banner: boolean): void {

            console.log('showshowshowshowshowshowshowshow')
            let adManager = manager.GameManager.getInstance().getAdManager();
            if (need_banner && !this.bannerImg) {
                this.setBannerAd();
                this.loadImg();
            }

            if (this.bannerImg != null) {
                this.bannerImg.visible = need_banner;
            }

            // if (!this.floatAd && adManager.float_ad && adManager.float_ad.length) {
            //     this.setFloatAd();
            //     manager.GameManager.getInstance().statisticManager.adStats(null, "float");
            // }
            if (!this.floatAd2 && adManager.float_ad2 && adManager.float_ad2.length) {
                this.setFloatAd2();
                manager.GameManager.getInstance().statisticManager.adStats(null, "gameFloat");
            }
            if (this.adList.length && !this.list) {
                for (let i = 0; i < this.adList.length; i++) {
                    this.createAd(i);
                }
            }
            this.visible = true;
            
            if (adManager.isChecking()) {
                console.log(888888888)
                this.visible = false;
            }
        }

        public hide(): void {
            console.log(9999999)
            this.visible = false;
        }

    }

}